<?php

/**
 * @category   Ac
 * @package    Application_Model
 * @copyright  Orient Network Ltd
 * @license    Closed
 * @author     Olivier Bregeras
 */
class Model_Location extends Ac_Model
{

    protected static  $_collection = 'location';
    //conngroup for user and company models
    protected static $_connectionGroup = 'accounts';
    //db for user and company
    protected static $_db = SHARED_ACCOUNTS_ENV;

    /**
     *
     * @var array define the requirements for this model
     */
    protected static $_requirements = array(
        //'un'  => array('Required', 'Validator:EmailAddress'), //Email
    );

    protected static $_transcript = array(
        '_id'   => '_id',
        'nm_en' => 'nm_en',
        'nm_tc' => 'nm_tc',
		'nm_sc' => 'nm_sc',
        'path'  => 'path',
    );

    protected static $internal_language;

    public function __construct($data = array(), $config = array())
    {
        $config['db'] = SHARED_ACCOUNTS_ENV;
        $config['collection'] = 'location';
        parent::__construct($data, $config);
    }


    public function __toString()
    {
        return (string)$this->name;
    }
    
    public function getParent()
    {
        return Model_Location::one(array('_id' => new MongoId($this->path[count($this->path) - 2])));
    }

    public function getParentTree()
    {
        $res = array();
        foreach($this->path as $cat) {
            $res[] = Model_Location::find($cat);
        }
        return $res;
    }

    public static function getAreaL1()
    {
        $area = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => array('$size' => 1)))->sort(array('path' => 1));

        foreach($attr as $k => $loc) {
            $area[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
        }

        return $area;
    }

    public static function getAreaL1Search()
    {
        $area = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => array('$size' => 1)))->sort(array('path' => 1));
        $area[''] = Zend_Registry::get('Zend_View')->t('All');

        foreach($attr as $k => $loc) {
            $area[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
        }

        return $area;
    }

    public static function getAreaL1PostAd()
    {
        $area = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => array('$size' => 1)))->sort(array('path' => 1));
        $area[''] = Zend_Registry::get('Zend_View')->t('Select');
        //$area[FLAG_CN] = Zend_Registry::get('Zend_View')->t('China');
        //$area[FLAG_ML_CN] = Zend_Registry::get('Zend_View')->t('Mainland China');

        foreach($attr as $k => $loc) {
            //$area[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
			$area[(string) $loc->_id] = $loc->{'nm_sc'};
        }

        return $area;
    }

    public static function getAreaL2($id)
    {
        $district = array();
        $path = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => new MongoId($id)))->sort(array('nm_'.self::getInternalLanguage()=>1));

        foreach($attr as $k => $loc) {
            $path = $loc->path;
            if (!empty($path[1])) {
                $district[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
            }
        }

        return $district;
    }

    public static function getAreaL2Search($id)
    {
        $district = array();
        $path = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => new MongoId($id)))->sort(array('nm_'.self::getInternalLanguage()=>1));
        $district[''] = Zend_Registry::get('Zend_View')->t('All');

        foreach($attr as $k => $loc) {
            $path = $loc->path;
            if (!empty($path[1])) {
                $district[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
            }
        }
        return $district;
    }

    public static function getAreaL2PostAd($id)
    {
        $district = array();
        $path = array();
        $attr = Model_Location::all(array('city' => CURRENT_CITY_ID, 'path' => new MongoId($id)))->sort(array('nm_'.self::getInternalLanguage()=>1));
        $district[''] = Zend_Registry::get('Zend_View')->t('Select');

        foreach($attr as $k => $loc) {
            $path = $loc->path;
            if (!empty($path[1])) {
                $district[(string) $loc->_id] = $loc->{'nm_'.self::getInternalLanguage()};
				$district[(string) $loc->_id] = $loc->{'nm_sc'};
            }
        }
        return $district;
    }

    public function __get($name)
    {
        if ($name == 'area_l1') {
            //$id = parent::__get('area');
            $res = Model_Location::find($this->path[0]);
            return $res;
        } elseif($name == 'city_name') {
            $city = parent::__get('city');
            return self::getCityName($city);
        } elseif($name == 'name') {
            return parent::__get('nm_'.self::getInternalLanguage());
        } else {
            return parent::__get($name);
        }
    }

    public function __isset($name) {
        $val = self::__get($name);
        return!empty($val) ? true : parent::__isset($name);
    }


    public static function setInternalLanguage($lng) 
    {
        self::$internal_language = $lng;
    }

    public static function getInternalLanguage() 
    {
		return 'sc';
		if (empty(self::$internal_language)) {
			if (!empty($_GET['lng'])) {
				self::$internal_language = $_GET['lng'];
			} else {
				self::$internal_language = Zend_Registry::get('current_language');
			}
        }
        return self::$internal_language;
    }
    public static function getCityName($id)
    {
        switch($id) {
            case CITY_BEIJING: $name = '北京';break;
            case CITY_GUANGZHOU: $name = '廣州';break;
            case CITY_HANGZHOU: $name = '杭州';break;
            case CITY_SHANGHAI: $name = '上海';break;
            case CITY_SHENZHEN: $name = '深圳';break;
            case CITY_HONGKONG: $name = '香港';break;
            default: $name = '';
        }
        return $name;
    }
}
